# app/api/routes/sessions.py
from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.orm import Session
from typing import List

from app.core.database import get_db
from app.schemas import schemas
from app.crud import crud
from app.api.routes.auth import get_current_user

router = APIRouter()


@router.post("/start")
def start_session(
    session_data: schemas.SessionStart,
    db: Session = Depends(get_db),
    current_user=Depends(get_current_user),
):
    result = crud.start_computer_session(db=db, session_data=session_data)
    if not result:
        raise HTTPException(status_code=400, detail="上机开始失败")
    return {"session_id": result.id_, "message": "上机开始成功"}


@router.post("/end")
def end_session(
    session_data: schemas.SessionEnd,
    db: Session = Depends(get_db),
    current_user=Depends(get_current_user),
):
    result = crud.end_computer_session(db, session_id=session_data.session_id)
    if not result:
        raise HTTPException(status_code=400, detail="上机结束失败")
    return {"message": "上机结束成功", "total_cost": result.total_cost}


@router.get("/active")
def get_active_sessions(
    db: Session = Depends(get_db), current_user=Depends(get_current_user)
):
    sessions = crud.get_active_sessions(db=db)
    
    # 丰富返回数据，包含机器和机房信息
    enriched_sessions = []
    for session in sessions:
        session_dict = {
            "id_": session.id_,
            "student_id": session.student_id,
            "machine_id": session.machine_id,
            "start_time": session.start_time,
            "hourly_rate": session.hourly_rate,
            "status": session.status,
        }
        
        # 添加学生信息
        if session.student:
            session_dict["student"] = {
                "id_": session.student.id_,
                "name": session.student.name,
                "student_id": session.student.student_id
            }
        
        # 添加机器和机房信息
        if session.machine:
            session_dict["machine"] = {
                "id_": session.machine.id_,
                "machine_number": session.machine.machine_number,
                "status": session.machine.status,
            }
            if session.machine.room:
                session_dict["machine"]["room"] = {
                    "id_": session.machine.room.id_,
                    "name": session.machine.room.name,
                    "location": session.machine.room.location,
                    "hourly_rate": session.machine.room.hourly_rate
                }
        
        enriched_sessions.append(session_dict)
    
    return enriched_sessions


@router.get("/students/{student_id}/sessions")
def get_student_sessions(
    student_id: int,
    db: Session = Depends(get_db),
    current_user=Depends(get_current_user),
):
    return crud.get_student_sessions(db=db, student_id=student_id)


@router.get("/history", response_model=List[schemas.SessionHistoryItem])
def get_session_history(
    skip: int = 0,
    limit: int = 100,
    db: Session = Depends(get_db),
    current_user=Depends(get_current_user),
):
    return crud.get_session_history(db=db, limit=limit, skip=skip)
